package com.baozun.util.items;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 * 解析XML文件得到用户定义的数据.用来配置用户一些下拉选择框.
 * 
 * @author zhouxiaozhou
 * 
 */
public class ParseSelectXmlUtil {

	public List<Item> getSelectItems(String filePath) {

		List<Item> result = new ArrayList<Item>();
		Document doc = null;
		try {
			InputStream input = this.getClass().getClassLoader().getResourceAsStream("xmls/"+filePath);
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			doc = db.parse(input);
			if ( doc != null ) {
				Node n = doc.getFirstChild();
				NodeList list = n.getChildNodes();
				for (int i = 0; i < list.getLength(); i++) {
					Node tn = list.item(i);
					NodeList il = tn.getChildNodes();
					String caption = null;
					String id = null;
					for (int j = 0; j < il.getLength(); j++) {
						Node tempNode = il.item(j);
						if ( tempNode.getNodeName().equals("caption") ) {
							try {
								caption = tempNode.getFirstChild().getNodeValue();
							} catch (Exception e) {
								System.out.println(tempNode.getNodeName());
							}
						} else if ( tempNode.getNodeName().equals("id") ) {
							id = tempNode.getFirstChild().getNodeValue();
						}
					}

					if ( id != null && caption != null ) {
						Item item = new Item(id, caption);
						result.add(item);
					}

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 通过xml文件获得字典项
	 * @param inputStream
	 * @return
	 */
	public List<Item> getSelectItems(InputStream inputStream) {

		List<Item> result = new ArrayList<Item>();
		Document doc = null;
		try {
			// InputStream input = this.getClass()
			// .getClassLoader().getResourceAsStream(filePath);
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			doc = db.parse(inputStream);
			if ( doc != null ) {
				Node n = doc.getFirstChild();
				NodeList list = n.getChildNodes();
				for (int i = 0; i < list.getLength(); i++) {
					Node tn = list.item(i);
					NodeList il = tn.getChildNodes();
					String caption = null;
					String id = null;
					for (int j = 0; j < il.getLength(); j++) {
						Node tempNode = il.item(j);
						if ( tempNode.getNodeName().equals("caption") ) {
							try {
								caption = tempNode.getFirstChild().getNodeValue();
							} catch (Exception e) {
								System.out.println(tempNode.getNodeName());
							}
						} else if ( tempNode.getNodeName().equals("id") ) {
							id = tempNode.getFirstChild().getNodeValue();
						}
					}

					if ( id != null && caption != null ) {
						Item item = new Item(id, caption);
						result.add(item);
					}

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
